<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: laiver.php 6 2005-07-11 23:14:47Z say $
 */
 
include("../db.php");
include("auth.php");
include("top.php");
include("rollerinclude.php");

echo "<h1>Laiver</h1>\n";

if ($_REQUEST['process'] == 'edit') {
	$id = $_REQUEST['id'];
	if (is_numeric($id))
		editForm($id);
} elseif ($_REQUEST['process'] == 'add') {
	editForm(0);
} elseif ($_REQUEST['process'] == 'insert') {
	$id = $_REQUEST['id'];
	$data=$_REQUEST['data'];

	$strfelt["Tittel"] = "Tittel";
	$strfelt["Undertittel"] = "Undertittel";
	$strfelt["Startdato"] = "Begynner (dato)";
	$strfelt["Sluttdato"] = "Slutter (dato)";
	$strfelt["Pamelding_start"] = "P�melding starter (dato)";
	$strfelt["Pamelding_slutt"] = "P�melding slutter (dato)";
	$strfelt["Roller_release"] = "Roller etc. off.gj. (dato)";
	$strfelt["Beskrivelse"] = "Beskrivelse";

	$intfelt["Offentlig"] = "Offentlig";
	
	$sql = "INSERT INTO Laiv (";
	foreach (array_keys($strfelt) as $felt)
		$sql .= "$felt, ";
	foreach (array_keys($intfelt) as $felt)
		$sql .= "$felt, ";
	
	$sql = substr($sql, 0, strlen($sql) - 2);

	$sql .= ") VALUES (";
	foreach (array_keys($strfelt) as $felt)
		$sql .= "'{$data[$felt]}', ";
	foreach (array_keys($intfelt) as $felt)
		$sql .= "{$data[$felt]}, ";
	
	$sql = substr($sql, 0, strlen($sql) - 2). ")";
	
	mysql_query($sql);
	if (mysql_errno()) {
		echo "<p>En feil oppstod under lagring av data. Dataene ble ikke lagret.</p>";
		echo "<p>".mysql_error().", $sql</p>";
	}
	
} elseif ($_REQUEST['process'] == 'update') {
	$id = $_REQUEST['id'];
	$sql = "UPDATE Laiv SET ";
	
	foreach($_REQUEST['data'] as $key=>$value) {
		$value=mysql_escape_string($value);
		if ($key == 'Offentlig')
			$sql .= "$key=$value, ";
		else
			$sql .= "$key='$value', ";
	}

	$sql = substr($sql, 0, strlen($sql) - 2);
	$sql .= " WHERE ID=$id";

	mysql_query($sql);
	if (mysql_errno()) {
		echo "<p>En feil oppstod under lagring av data. Dataene ble ikke lagret.</p>";
		echo "<p>".mysql_error()." ($sql)</p>";
	}
} elseif ($_REQUEST['process'] == 'del') {
	printDelConfirm();
} elseif ($_REQUEST['process'] == 'del2') {
} else {
	printOversikt();
}

include("bottom.php");


function printOversikt() {
	$sql = "SELECT *, DATE_FORMAT(Startdato, '%d.%m') AS Start, DATE_FORMAT(Sluttdato, '%d.%m.%Y') AS Slutt FROM Laiv ORDER BY Startdato";
	$laiver = mysql_query($sql);
	?>
	<table class="gridded">
		<tr>
			<th>Navn</th>
			<th>Off</th>
			<th>Dato</th>
			<th>Endre</th>
			<th>Slette</th>
		</tr>
	<?
	while ($laiv = mysql_fetch_assoc($laiver)) {
		?>
		<tr>
			<td><?=$laiv['Tittel']?></td>
			<td><? if ($laiv['Offentlig']) echo "J"; else echo "N"; ?></td>
			<td><?=$laiv['Start']?> - <?=$laiv['Slutt']?></td>
			<td><a href="laiver.php?process=edit&id=<?=$laiv['ID']?>">[endre]</a></td>
			<td><a href="laiver.php?process=del&id=<?=$laiv['ID']?>">[slett]</a></td>
		</tr>
		<?
	}
	echo "</table>\n";

	echo "<p><a href=\"laiver.php?process=add\">Legg til laiv</a></p>\n";
}



function editForm($id) {
	if ($id == 0) {
		$action="Ny laiv";
		$process="insert";

		$data['Startdato'] = date("Y-m-d");
		$data['Sluttdato'] = date("Y-m-d");
		
		$data['Pamelding_start'] = date("Y-m-d");
		$data['Pamelding_slutt'] = date("Y-m-d");
		
		$data['Roller_release'] = date("Y-m-d");
	} else {
		$action="Rediger";
		$process="update";

		$sql="SELECT * FROM Laiv WHERE ID=$id";
		$res=mysql_query($sql);

		if (mysql_num_rows($res) != 1) 
			return false;
		else {
			$data=mysql_fetch_assoc($res);
		}
	}

	$textfields["Tittel"] = "Tittel";
	$textfields["Undertittel"] = "Undertittel";
	$textfields["Startdato"] = "Begynner (dato)";
	$textfields["Sluttdato"] = "Slutter (dato)";
	$textfields["Pamelding_start"] = "P�melding starter (dato)";
	$textfields["Pamelding_slutt"] = "P�melding slutter (dato)";
	$textfields["Roller_release"] = "Roller etc. off.gj. (dato)";

	$textareas["Beskrivelse"] = "Beskrivelse";

	?>
	<h2><?=$action?></h2>

	<p>Alle datoer skal angis i ISO-format, dvs ����-MM-DD.</p>

	<form method="post" action="laiver.php">
	<input type="hidden" name="process" value="<?=$process?>"/>
	<input type="hidden" name="id" value="<?=$id?>"/>
	<table class="gridded">
		<?
		foreach ($textfields as $field=>$forklaring) {
			?>
			<tr>
			<th><?=$forklaring?></th>
			<td><input name="data[<?=$field?>]" value="<?=$data[$field]?>">
			</tr>
			<?
		}
		?>
		<?
		foreach ($textareas as $field=>$forklaring) {
			?>
			<tr>
			<th><?=$forklaring?></th>
			<td><textarea name="data[<?=$field?>]" cols="40" rows="20"><?=$data[$field]?></textarea></td>
			</tr>
			<?
		}
		?>
		<tr>
			<th>Offentlig</th>
			<td><?
			$yesextra="";
			$noextra="";
			if ($data['Offentlig'])
				$yesextra=" CHECKED";
			else
				$noextra=" CHECKED";
			?>
			<input type="radio" name="data[Offentlig]" value="1" <?=$yesextra?>>
			Ja<br/>
			<input type="radio" name="data[Offentlig]" value="0" <?=$noextra?>>
			Nei
			</td>
		</tr>
			
		<tr>
			<th>Lagre</th>
			<td><input type="submit" value="Lagre">
		</tr>
		<tr>
			<th>Tilbakestill</th>
			<td><input type="reset" value="Tilbakestill">
		</tr>
	</table>
	</form>
	<?
}


function printDelConfirm() {
	?>
	<h2>Slette laiv</h2>
	<p>Er du helt sikker p� at du vil slette denne laiven? Dette
	medf�rer at f�lgende elementer slettes:</p>

	<ul>
		<li>Laivinformasjonen</li>
		<li>P�meldinger</li>
		<li>Deltakerinformasjon</li>
		<li>Innloggingslogger</li>
		<li>Gruppe-, v�pen- og intrigeinformasjon</li>
		<li>Evalueringsskjema og -svar</li>
		<li>Roller og tilordninger</li>
		<li>Dokumenter og dokumenttilganger</li>
		<li>Nyheter</li>
		<li>Stikkord</li>
		<li>Bilder</li>
		<li>E-poster</li>
	</ul>

	<p>Et alternativ til � slette laiven, er � gj�re den
	"ikke offentlig". Dette vil ha samme effekt overfor
	verden, men vil ikke slette noen data.</p>

	<form method="post" action="laiver.php">
		<input type="hidden" name="process" value="del2">
		<input type="hidden" name="id" value="<?=$id?>">
		<input type="submit" value="Slett">
	</form>
	<form method="post" action="laiver.php">
		<input type="submit" value="Angre">
	</form>
	<?
}
?>
